<template>
  <div>
    <div>【大区异常处理】</div>
    <div ref="target" class="w-full h-full"></div>
  </div>
</template>
<script setup>
import { ref, onMounted, watch } from "vue";
import * as echarts from "echarts";
const props = defineProps({
  data: {
    type: Object,
    required: true,
  },
});
console.log(props.data);

// 1. 初始化echarts实例
let myChart = null;
const target = ref(null);
onMounted(() => {
  myChart = echarts.init(target.value);
  renderChart();
});

const getSeriesData = () => {
  const series = [];
  props.data.abnormals.forEach((item, index) => {
    // 上层
    series.push({
      name: item.name,
      type: "pie",
      clockWise: false, // 逆时针
      hoverAnimation: false, // 取消鼠标移入动画
      radius: [73 - index * 15 + "%", 68 - index * 15 + "%"], // 内半径和外半径
      center: ["55%", "55%"], // 中心点
      label: {
        show: false,
      },
      data: [
        {
          value: item.value,
          name: item.name,
        },
        {
          value: 1000,
          itemStyle: {
            color: "rgba(0, 0, 0, 0)",
            borderWidth: 0,
          },
          tooltip: {
            show: false,
          },
          hoverAnimation: false,
        },
      ],
    });
    // 下层
    series.push({
      name: item.name,
      type: "pie",
      silent: true, // 鼠标事件不触发
      z: 1, // 层级
      clockWise: false, // 逆时针
      hoverAnimation: false,
      radius: [73 - index * 15 + "%", 68 - index * 15 + "%"], // 内半径和外半径
      center: ["55%", "55%"], // 中心点
      label: {
        show: false,
      },
      data: [
        {
          value: 7.5, // 底线
          itemStyle: {
            color: "rgb(3, 31, 62)",
            borderWidth: 0,
          },
          tooltip: {
            show: false,
          },
          hoverAnimation: false,
        },
        {
          value: 2.5, //透明区
          itemStyle: {
            color: "rgba(0, 0, 0, 0)",
            borderWidth: 0,
          },
          tooltip: {
            show: false,
          },
          hoverAnimation: false,
        },
      ],
    });
  });
  return series;
};

// 2. 构建options配置对象
const renderChart = () => {
  const options = {
    // 图例配置
    legend: {
      show: true,
      icon: "circle", // 图例形状
      top: "14%",
      left: "60%",
      data: props.data.abnormals.map((item) => item.name),
      width: -5, // 用于图例竖向展示的配置
      itemWidth: 10,
      itemHeight: 10,
      itemGap: 6, // 图例之间的间距
      textStyle: {
        fontSize: 12,
        lineHeight: 5,
        color: "rgba(255, 255,255, 0.8)",
      },
    },
    // 提示层
    tooltip: {
      show: true,
      trigger: "item", // 数据项触发
      formatter: "{a}<br>{b}:{c} ({d}%)", // a: 系列名称，b: 数据项名称，c: 数据值, d: 百分比
    },
    yAxis: [
      {
        type: "category",
        inverse: true,
        axisLine: {
          show: false,
        },
      },
    ],
    xAxis: [
      {
        show: false,
      },
    ],
    series: getSeriesData(),
  };
  myChart.setOption(options);
};

watch(() => props.data, renderChart);
// 3. 通过实例.setOption(option)渲染图表
</script>
<style lang="scss" scoped>
</style> 